import 'package:floor/floor.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/models/base_model.dart';
import 'package:share_fit/generated/locales.g.dart';

@Entity(tableName: 'step', indices: [Index(value: ['calendar', 'address'])])
class StepModel extends BaseModel {
  /// 日期时间，格式 ：yyyy-MM-dd HH:mm
  @ColumnInfo(name: 'calendar_time')
  late String? calendarTime;

  /// 日期，格式 ：yyyyMMdd
  late String? calendar;

  /// 时间， 单位： 分钟。比如 930 = 15:30
  late int? time;

  /// 开始时间，格林尼治时间，单位：秒
  @ColumnInfo(name: 'start_time')
  late int? startTime;

  /// 结束时间，格林尼治时间，单位：秒
  @ColumnInfo(name: 'end_time')
  late int? endTime;

  /// 运动步数，最小计步单位为1步
  late int? step;

  /// 运动卡路里消耗值，单位：千卡
  late double? calories;

  /// 运动距离，单位：米
  late int? distance;

  /// 运动类型，0为走路，1为跑步
  late int? type;

  ///
  late int? offset;

  StepModel({
    super.id,
    super.address,
    super.dateCreated,
    super.dateModified,
    this.calendarTime,
    this.calendar,
    this.time,
    this.startTime,
    this.endTime,
    this.step,
    this.calories,
    this.distance,
    this.type,
    this.offset,
  });

  @override
  bool from(Map map) {
    // TODO: implement from
    throw UnimplementedError();
  }

  /// 获取当天的汇总数据
  TodaySummary get summary => TodaySummary(stepSum: step ??= 0, distanceSum: distance ??= 0, caloriesSum: (calories ??= 0).toInt());

  @override
  List<Object?> get props => [];

  @override
  bool? get stringify => true;
}

class TodaySummary {
  late int stepSum = 0;
  late int caloriesSum = 0;
  late int distanceSum = 0;
  late int stepTarget = 0;
  late int caloriesTarget = 0;
  late int distanceTarget = 0;
  late int lengthUnit = 0;

  TodaySummary({this.stepSum = 0, this.distanceSum = 0, this.caloriesSum = 0});

  setTarget(int stepTarget, int distanceTarget, int caloriesTarget, int lengthUnit) {
    this.stepTarget = stepTarget;
    this.caloriesTarget = caloriesTarget;
    this.distanceTarget = distanceTarget;
    this.lengthUnit = lengthUnit;
  }

  /// 获取步数文本
  get getStepSumText => '$stepSum';

  /// 获取距离文本
  get getDistanceSumText {
    double temp = distanceSum == 0 ? 0 : (distanceSum / 1000);
    if (lengthUnit == 1) {
      temp = temp * 0.62137;
      return '${temp.toStringAsFixed(2)} ${LocaleKeys.length_unit_mi.ts}';
    } else {
      return '${temp.toStringAsFixed(2)} ${LocaleKeys.length_unit_km.ts}';
    }
  }

  /// 获取卡路里文本
  get getCaloriesSumText {
    return '$caloriesSum ${LocaleKeys.thermal_unit_kcal.ts}';
  }

  get getStepProcessValue {
    return stepTarget <= 0 ? 0.0 : (stepSum / stepTarget).toDouble();
  }

  get getCalorieProcessValue {
    return caloriesTarget <= 0 ? 0.0 : (caloriesSum / caloriesTarget).toDouble();
  }

  get getDistanceProcessValue {
    return distanceTarget <= 0 ? 0.0 : (distanceSum / distanceTarget/1000).toDouble();
  }
}
